import { ActRegex } from "./parse.js";
import { GameChatMsgType } from "../../type/gameType.js";

// 将工具参数类型转换为JS类型
export function typeConversion(type: string): string {
  switch (type) {
    case "int":
    case "float":
      return "number";
    case "bool":
      return "boolean";
    case "string":
      return "string";
  }
  throw new Error(`unknow type: ${type}`);
}

// 移除输入字符串两段的引号和空白 支持 "" 和 ''
export function unquote(inString: string): string {
  inString = inString.trim();
  if (
    (inString[0] === inString[inString.length - 1] &&
      (inString[0] === '"' || inString[0] === "'")) ||
    (inString[0] === "“" && inString[inString.length - 1] === "”") ||
    (inString[0] === "‘" && inString[inString.length - 1] === "’")
  ) {
    inString = inString.substring(1, inString.length - 1);
  }
  return inString;
}

// 省略字符串
export function truncateMiddle(
  str: string,
  startLength: number = 3,
  endLength: number = 3,
  ellipsis: string = "..."
): string {
  if (str.length <= startLength + endLength) {
    return str;
  }
  const start = str.substring(0, startLength);
  const end = str.substring(str.length - endLength);
  return `${start}${ellipsis}${end}`;
}

// 移除工具调用字符串
export function removeActCallString(inString: string): string {
  return inString.replace(new RegExp(ActRegex.source, "g"), "").trim();
}

// 工具调用字符串
export function formatActCallString(msg: string, toolName?: string): string {
  let actMessage: GameChatMsgType | string = {
    role: "system",
    type: "actCall",
    content: msg,
    name: toolName,
  };
  return `[${actMessage.role}]{${actMessage.type}}${
    actMessage.name ? "<" + actMessage.name + ">" : ""
  }${actMessage.content}`;
}
